import socket
import threading
import time

# 创建套接字
s = socket.socket()

# 连接服务器
s.connect(("bemfa.com", 8344))
server = s.makefile()

def heartbeat():
    while True:
        # 发送心跳消息
        s.sendall("ping\r\n".encode())
        print(server.readline())
        time.sleep(30)

# 创建心跳线程并启动
t = threading.Thread(target = heartbeat)
t.start()

# 订阅主题
s.sendall("cmd=1&uid=4e78c607dbbc9f4d6b5c16ffa5622fb7&topic=bell\r\n".encode())
print(server.readline())

while True:
    print(server.readline())
